#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>

int main()
{
    pid_t pid;
    pid = fork();

	//fork() will return twice
    if(pid == 0) {
		//taskset -c [number of CPU core] [your children process]
        if(execl("/usr/bin/taskset","taskset","-c", "0","./app1", NULL) < 0) {
            perror("excel failed");
            exit(1);
        }
    } else if(pid > 0) {
        printf("process1 id:%d\n",pid);
    } else {
        perror("fork App 1 failed:");
        exit(1);
    }

    pid = fork();
    if(pid == 0) {
        if(execl("/usr/bin/taskset","taskset","-c", "0","./app2", NULL) < 0) {
            perror("excel failed");
            exit(1);
        }
    } else if(pid > 0) {
        printf("process2 id:%d\n",pid);
    } else {
        perror("fork App 2 failed:");
        exit(1);
    }

	//父进程需回收子进程
	while(1){
		//如果没有子进程或者信号中断了会返回-1
		if(wait(NULL) == -1){
			//要排除信号中断的情况
			if(errno == EINTR)
				continue;
		}
		break;
	}
	printf("children exit\n");

    return 0;
}
